home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 52 / Amiga Format AFCD52 (Issue 136, May 2000).iso / -in_the_mag- / multitasking / monitors / tasky / source / tasklist.c < prev    next >
C/C++ Source or Header  |  2000-03-05  |  5KB  |  256 lines

  1. /*
  2.  * Routines to copy task list to another list.
  3.  * For TaskY and xDataPrefs.
  4.  * Many specific changes for TaskY.
  5.  * 
  6.  * Martin W. Scott, 23 March 1993.
  7.  */
  8. #include <exec/types.h>
  9. #include <exec/tasks.h>
  10. #include <exec/memory.h>
  11. #include <exec/execbase.h>
  12. #include <dos/dos.h>
  13. #include <dos/dosextens.h>
  14. #include <proto/exec.h>
  15. #include <proto/dos.h>
  16. #include <string.h>
  17. #include "sprintf.h"
  18. extern struct ExecBase *SysBase;
  19.  
  20. #include "ptask.h"
  21. static LIST tmplist;
  22. LIST ptlist;
  23. LONG num_ptasks;
  24.  
  25.  
  26. void
  27. InitPTaskList()
  28. {
  29.     NewList(&ptlist);
  30. }
  31.  
  32. /* clear list, freeing memory */
  33. void
  34. DeletePTaskList()
  35. {
  36.     PTASK *pt;
  37.  
  38.     Forbid();
  39.     while (pt = (PTASK *)RemHead(&ptlist))
  40.     {
  41.     FreeVec(pt->pt_Node.ln_Name);
  42.     FreeVec(pt->pt_OrigName);
  43.     FreeVec(pt);
  44.     }
  45.     Permit();
  46. }
  47.  
  48. /* allocate memory region of size len+1, copy string to it */
  49. /* tasky specific: pad string out with spaces and priority at end */
  50. #define WIDTH 27        /* name width */
  51. #define FULLWIDTH (WIDTH+4)    /* total width -- 4 chars for priority */
  52. char *
  53. AllocStr(UBYTE len, BYTE pri, UBYTE *str)
  54. {
  55.     char *newstr;
  56.     UWORD i;
  57.     BOOL trunc = FALSE;
  58.  
  59.     if (len > WIDTH)            /* truncate length? */
  60.     len = WIDTH, trunc = TRUE;
  61.  
  62.     if (newstr = AllocVec(FULLWIDTH+1,0L))
  63.     {
  64.     CopyMem(str,newstr,len);
  65.     for (i = len; i < WIDTH; i++)
  66.         newstr[i]=' ';
  67.     if (trunc)
  68.         newstr[WIDTH-1] = '$';
  69.     SPrintf(&newstr[WIDTH], "%4ld", (long)pri);
  70.     }
  71.     return newstr;
  72. }
  73. #define FreeStr(s)    FreeVec(s)
  74.  
  75. /* insert node in sorted position in list (sort by name) */
  76. void
  77. InsertInPlace(LIST *list, NODE *new)
  78. {
  79.     NODE *n;
  80.  
  81.     for (n = list->lh_Head; n->ln_Succ; n = n->ln_Succ)
  82.     {
  83.     if (stricmp(n->ln_Name, new->ln_Name) >= 0)
  84.     {
  85.         Insert(list,new,n->ln_Pred);
  86.         return;
  87.     }
  88.     }
  89.     /* got here - must be last node */
  90.     AddTail(list, new);
  91. }
  92.  
  93. /* create a sorted list from a list */
  94. void
  95. SortList(LIST *from, LIST *to)
  96. {
  97.     NODE *n;
  98.  
  99.     while (n = RemHead(from))
  100.         InsertInPlace(to, n);
  101. }
  102.  
  103. /* set up pnode from given task-node */
  104. BOOL
  105. InitNode(PTASK *pt, NODE *n, char *name, UBYTE len)
  106. {
  107.     UWORD i;
  108.  
  109.     if (pt->pt_OrigName = AllocVec(i=(strlen(n->ln_Name)+1), 0L))
  110.     {
  111.     CopyMem(n->ln_Name, pt->pt_OrigName, i);
  112.     if (pt->pt_Node.ln_Name = AllocStr(len, n->ln_Pri, name))
  113.     {
  114.         AddHead(&tmplist, pt);
  115.         pt->pt_Task = (TASK *)n;
  116.         pt->pt_TaskPri = n->ln_Pri;
  117.         return TRUE;
  118.     }
  119.     FreeVec(pt->pt_OrigName);
  120.     }
  121.     return FALSE;
  122. }
  123.  
  124. /* add a new task to our list */
  125. BOOL
  126. AddTaskToPTaskList(TASK *t)
  127. {
  128.     UBYTE cmdname[300];
  129.     PTASK *pt;
  130.  
  131.     num_ptasks++;            /* increment count */
  132.     if (pt = AllocPTask())        /* get a new PTask */
  133.     {
  134.     PROC *proc = (PROC *)t;    /* proc from task */
  135.  
  136.     /* task or non-cli process */
  137.     if (t->tc_Node.ln_Type != NT_PROCESS || proc->pr_TaskNum == 0)
  138.     {
  139.         char *name = t->tc_Node.ln_Name;
  140.         if (InitNode(pt, t, name, strlen(name)))
  141.         return TRUE;
  142.     }
  143.     else    /* cli-process...fun and games */
  144.     {
  145.         struct CommandLineInterface *cli = BADDR(proc->pr_CLI);
  146.         UBYTE *bstr = BADDR(cli->cli_CommandName);
  147.         UBYTE len;
  148.  
  149.         if (bstr && bstr[0]) {    /* a command is loaded */
  150.         len = bstr[0];
  151.         bstr = &bstr[1];
  152.         } else {
  153.         bstr = "No command loaded";
  154.         len = 17;
  155.         }
  156.  
  157.         CopyMem(bstr, cmdname, len);
  158.         SPrintf(&cmdname[len]," [CLI %ld]", proc->pr_TaskNum);
  159.  
  160.         if (InitNode(pt, t, cmdname, len+strlen(&cmdname[len])))
  161.         return TRUE;
  162.     }
  163.  
  164.     FreePTask(pt);    /* memory allocation failure... */
  165.     }
  166.     return FALSE;
  167. }
  168.  
  169. /* add tasks from supplied task list to our list */
  170. /* return TRUE if okay, FALSE if memory allocation failed */
  171. BOOL
  172. AddListToPTaskList(LIST *tl)
  173. {
  174.     TASK *t;
  175.  
  176.     /* scan the list, adding processes to the plist */
  177.     for (t = (TASK *)tl->lh_Head; t->tc_Node.ln_Succ; t = (TASK *)t->tc_Node.ln_Succ)
  178.     {
  179.     if (!AddTaskToPTaskList(t))
  180.     {
  181.         Permit();
  182.         return FALSE;
  183.     }
  184.     } /* for */
  185.  
  186.     return TRUE;
  187. }
  188.  
  189. /* re-read PTask list, deleting old one if required */
  190. /* return success */
  191. BOOL
  192. RefreshPTaskList()
  193. {
  194.     static BOOL firsttime = TRUE;
  195.     BOOL rc = FALSE;
  196.  
  197.     if (firsttime)
  198.     {
  199.     InitPTaskList();
  200.     NewList(&tmplist);
  201.     firsttime = FALSE;
  202.     }
  203.     else
  204.     DeletePTaskList();
  205.  
  206.     num_ptasks = 0;    /* reset count */
  207.  
  208.     /* want to be disabled for as short a period as possible, so
  209.        build a tmp list, then copy to another in order */
  210.  
  211.     Disable();
  212.     if (AddListToPTaskList(&SysBase->TaskReady) &&
  213.         AddListToPTaskList(&SysBase->TaskWait) &&
  214.     AddTaskToPTaskList(SysBase->ThisTask))
  215.     rc = TRUE;
  216.     Enable();
  217.  
  218.     SortList(&tmplist, &ptlist);
  219.  
  220.     if (!rc)
  221.     DeletePTaskList();
  222.     return rc;
  223. }
  224.  
  225. /* check list for given task - assumes forbidden */
  226. TASK *
  227. ScanList(LIST *list, PTASK *pt)
  228. {
  229.     TASK *t;
  230.  
  231.     for (t = (TASK *)FindName(list, pt->pt_OrigName);
  232.          t && t != pt->pt_Task;
  233.      t = (TASK *)FindName((LIST *)t, pt->pt_OrigName))
  234.          ;
  235.  
  236.     return t;
  237. }
  238.  
  239. /* check task lists for given task */
  240. TASK *
  241. GetOrigTask(PTASK *pt)
  242. {
  243.     TASK *t;
  244.  
  245.     Disable();
  246.     t = ScanList(&SysBase->TaskReady, pt);
  247.     if (!t)
  248.         t = ScanList(&SysBase->TaskWait, pt);
  249.     Enable();
  250.     if (!t) /* is it us? */
  251.         if (pt->pt_Task == SysBase->ThisTask)
  252.         t = SysBase->ThisTask;
  253.  
  254.     return t;
  255. }
  256.